const Common = require('wy-Common')
cc.Class({
    name : 'wy-QQPlay',
    extends:require('wy-PlatformBase'),
    init(){
        this._super();
        this.os = 'qqplay'
        this.isQQPlay = true
        this.autoFileIndex = 0;
        console.log('qqplay 初始化')
        this.exception = new BK.Game({
          onException:function(app){
             //app.errorMessage为错误信息，app.errorStacktrace为堆栈信息
              BK.Script.log(1, 0, "msg = " + app.errorMessage() + ", stack = " + app.errorStacktrace());
          }
        });

    },
    login:function login(cb){
        console.log('qq play login')
        BK.QQ.fetchOpenKey(function (errCode, cmd, data) {
          if (errCode == 0) {
               var openKey = data.openKey;
           }
        });
        var self = this
        setTimeout(function(){
          self._getUserInfo(cb);
        },100)
    },
    _getUserInfo:function _getUserInfo(finishcb){
        var self = this;
        let async = new CCAsync();
        async.parallel([function(cb){
          BK.MQQ.Account.getHeadEx(GameStatusInfo.openId, function(oId, imgPath) {

            BK.fileSystem.readFile({'file':[{path:imgPath}],
                'success': function (res) {
                  var d = res[0]
                  cb(null,d.data)
                },'fail':function(){
                    BK.Script.log(1, 1, "异步加载文件调用失败 ");
                  cb('get head file error')
                }
            });
          }.bind(this))
        },function(cb){
          BK.MQQ.Account.getNick(GameStatusInfo.openId,function(oid,nick){
            cb(null,nick)
          });
        },function(cb1){
          BK.QQ.fetchOpenKey(function (errCode, cmd, data) {
            cb1(errCode,data)
          });
        }],function(err,results){
          cc.log('_getUserInfo finish')
          if(err){
            self._getUserInfo(finishcb);
            return 
          }
          finishcb({
            openid:GameStatusInfo.openId,
            nickName : results[1],
            imgData : Common.arrayBufferToBase64(results[0]),
            openKey : results[2].openKey
          })
        })
    },
    loadRemoteImg(remoteUrl,callback){
      var self = this;
      var idx = self.autoFileIndex++;
      var httpReq = new BK.HttpUtil(remoteUrl);
      httpReq.setHttpMethod("get");
      httpReq.requestAsync(function(buffer, status) {
        if (200 === status){
          var url = qqPlayDownloader.GameSandBox_ROOT + "/temp/" + idx+'.png';
          BK.FileUtil.writeBufferToFile(url, buffer);
          callback(null,url)
        }
      }.bind(this));
    },
    share:function share(title, queryObj, successCallBack,type = 1){
      let query = Object.keys(queryObj).map((key) => { return key + '=' + queryObj[key]; }).join("&");
      
      let imageUrl = 'https://oss.073c.com/paopao/shareImg/sharetype';
      imageUrl = typeof (type) == 'string' ? type : (imageUrl + type + '.jpg');
      console.log('分享的参数', imageUrl);
      BK.Share.share({
        qqImgUrl: imageUrl,
        isToFriend:true,
        summary: title,
        extendInfo:query
      });
    },
    // showLoading(text){
    //   BK.UI.showLoading({title:text});
    // },
    // hideLoading(text){
    //   BK.UI.hideLoading({title:text});
    // },
    showToast(text){
      BK.UI.showToast({title:text});
    }
});
